package com.hq.fxh.No3_TuiGuang.Fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.hq.fxh.Modle.GetPushDataRes;
import com.hq.fxh.No0_ChengQu.Fragment.BaseFragment;
import com.hq.fxh.No3_TuiGuang.Adaoter.PushStatisticDataFragmentAdapter;
import com.hq.fxh.R;
import com.hq.fxh.Utils.ScreenManager;

import java.util.List;



public class PushStatisticDataFragment extends BaseFragment {
    private LinearLayout ll_expose;
    private RecyclerView recyclerView;
    private HorizontalScrollView sv_expose;
    private PushStatisticDataFragmentAdapter adapter;
    private TextView type;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_push_statistic_data;
    }

    @Override
    protected void findView(View view) {
        ll_expose = (LinearLayout) view.findViewById(R.id.ll_expose);
        sv_expose = (HorizontalScrollView) view.findViewById(R.id.sv_expose);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        type = (TextView) view.findViewById(R.id.type);
    }

    @Override
    protected void initData() {
        Bundle bundle = getArguments();
        LinearLayoutManager layoutManager = new LinearLayoutManager(myActivity);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
    }

    public void setData(int ecportOrClick, int dateType, List<GetPushDataRes.DataBean> dataList) {
        int lookmax = dataList.get(0).getLook();
        int clickmax = dataList.get(0).getClick();
        for (int i = 0; i < dataList.size(); i++) {
            if (lookmax < dataList.get(i).getLook()) {
                lookmax = dataList.get(i).getLook();
            }
            if (clickmax < dataList.get(i).getClick()) {
                clickmax = dataList.get(i).getClick();
            }
        }
        if (ecportOrClick == 0) {//曝光
            type.setText("曝光明细");
            createChildLinearLayout(ecportOrClick, dateType, lookmax, dataList);
        } else if (ecportOrClick == 1) {//点击
            type.setText("点击明细");
            createChildLinearLayout(ecportOrClick, dateType, clickmax, dataList);
        }
    }

    TextView tempTextView;//临时变量
    ImageView tempImage;//临时变量
    ImageView tempData;

    private void createChildLinearLayout(final int ecportOrClick, final int dateType, final int max, final List<GetPushDataRes.DataBean> dataList) {
        ll_expose.removeAllViews();
        Activity context = getActivity();
        int maxHeight = 130;

        for (int i = dataList.size()-1; i >=0; i--) {
            LinearLayout myLinear = new LinearLayout(context);
            myLinear.setOrientation(LinearLayout.HORIZONTAL);

            LinearLayout.LayoutParams linearLp = new LinearLayout.LayoutParams(80, LayoutParams.MATCH_PARENT);
            linearLp.setMargins(0, 0, 0, 0);//左上右下
            myLinear.setTag(i);
            ll_expose.addView(myLinear, linearLp);

            //LinearLayout内嵌套RelativaLayout
            RelativeLayout myRela = new RelativeLayout(myActivity);
            LinearLayout.LayoutParams relaLp = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1);
            myRela.setId(R.id.parent);
            myRela.setGravity(Gravity.CENTER_HORIZONTAL);
            myLinear.addView(myRela, relaLp);

            //日期
            RelativeLayout.LayoutParams textViewLp1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            textViewLp1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            textViewLp1.addRule(RelativeLayout.CENTER_HORIZONTAL, R.id.parent);
            textViewLp1.setMargins(0, 8, 0, 14);//左上右下
            final TextView textView = new TextView(myActivity);
            textView.setTextSize(12);
            textView.setTextColor(getResources().getColor(R.color.black5));
            textView.setId(R.id.date);
            int date = Integer.parseInt(dataList.get(i).getDate());
            if (dateType == 1) {//1今日 2本月
                date = date + 1;
            }
            if (date == 1 || date % 6 == 0 || date == dataList.size()) {
                textView.setText(date + "");
            }
            myRela.addView(textView, textViewLp1);

            //分隔线
            RelativeLayout.LayoutParams viewLp = new RelativeLayout.LayoutParams(textView.getMeasuredWidth(), LayoutParams.WRAP_CONTENT);
            viewLp.addRule(RelativeLayout.ABOVE, R.id.date);
            viewLp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, R.id.parent);
            viewLp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, R.id.parent);
            View view = new View(myActivity);
            view.setId(R.id.split);
            view.setBackgroundColor(getResources().getColor(R.color.black_text));
            viewLp.height = 3;
            float density = context.getResources().getDisplayMetrics().density;
            viewLp.width = (int) (density * 45);
            myRela.addView(view, viewLp);

            //实高度
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.ABOVE, R.id.split);
            lp.addRule(RelativeLayout.CENTER_HORIZONTAL, R.id.parent);
            final ImageView imageView = new ImageView(myActivity);
            imageView.setId(R.id.img);
            lp.width = 14;
            int a = 0;
            if (ecportOrClick == 0) {
                a = (int) ((maxHeight * density * 0.85 / max) * dataList.get(i).getLook());
            } else if (ecportOrClick == 1) {
                a = (int) ((maxHeight * density * 0.85 / max) * dataList.get(i).getClick());
            }
            lp.height = a;
            imageView.setBackgroundColor(myActivity.getResources().getColor(R.color.gray10));
            myRela.addView(imageView, lp);

            //补充高度
            RelativeLayout.LayoutParams lph = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            lph.addRule(RelativeLayout.ABOVE, R.id.img);
            lph.addRule(RelativeLayout.CENTER_HORIZONTAL, R.id.parent);
            ImageView imageView2 = new ImageView(myActivity);
            imageView2.setId(R.id.img2);
            lph.width = 14;
            int h = 0;
            if (ecportOrClick == 0) {
                h = (int) (maxHeight * density - ((maxHeight * density * 0.85 / max) * dataList.get(i).getLook()));
            } else if (ecportOrClick == 1) {
                h = (int) (maxHeight * density - ((maxHeight * density * 0.85 / max) * dataList.get(i).getClick()));
            }
            lph.height = h;
            imageView2.setBackgroundColor(myActivity.getResources().getColor(R.color.gray_new2));
            myRela.addView(imageView2, lph);

            //倒三角
            RelativeLayout.LayoutParams imgph = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            imgph.addRule(RelativeLayout.ABOVE, R.id.img2);
            imgph.addRule(RelativeLayout.CENTER_HORIZONTAL, R.id.parent);
            imgph.setMargins(0, 0, 8, 6);
            imgph.width = 12;
            imgph.height = 8;
            final ImageView imageView3 = new ImageView(myActivity);
            imageView3.setId(R.id.img3);
            imageView3.setVisibility(View.GONE);
            imageView3.setImageResource(R.mipmap.arrow_d_hover);
            myRela.addView(imageView3, imgph);

            //数据
            RelativeLayout.LayoutParams textViewLp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            textViewLp.addRule(RelativeLayout.ABOVE, R.id.img3);
            textViewLp.addRule(RelativeLayout.CENTER_HORIZONTAL, R.id.parent);
            textViewLp.setMargins(0, 0, 8, 6);
            final TextView tvcount = new TextView(myActivity);
            tvcount.setTextSize(11);
            tvcount.setVisibility(View.GONE);
            tvcount.setTextColor(getResources().getColor(R.color.rank_red));
            if (ecportOrClick == 0) {
                tvcount.setText(dataList.get(i).getLook() + "");
            } else {
                tvcount.setText(dataList.get(i).getClick() + "");
            }
            myRela.addView(tvcount, textViewLp);


            if (i == 0) {
                tvcount.setVisibility(View.VISIBLE);
                if (tempTextView != null) {
                    tempTextView.setVisibility(View.GONE);
                }
                tempTextView = tvcount;

                imageView3.setVisibility(View.VISIBLE);
                if (tempImage != null) {
                    tempImage.setVisibility(View.GONE);
                }
                tempImage = imageView3;

                imageView.setBackgroundColor(myActivity.getResources().getColor(R.color.new_red));
                if (tempData != null) {
                    tempData.setBackgroundColor(myActivity.getResources().getColor(R.color.gray4));
                }
                tempData = imageView;
            }

            final int finalI = i;
            myRela.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    tvcount.setVisibility(View.VISIBLE);
                    if (tempTextView != null) {
                        tempTextView.setVisibility(View.GONE);
                    }
                    tempTextView = tvcount;

                    imageView3.setVisibility(View.VISIBLE);
                    if (tempImage != null) {
                        tempImage.setVisibility(View.GONE);
                    }
                    tempImage = imageView3;

                    imageView.setBackgroundColor(myActivity.getResources().getColor(R.color.new_red));
                    if (tempData != null) {
                        tempData.setBackgroundColor(myActivity.getResources().getColor(R.color.gray4));
                    }
                    tempData = imageView;
                    setPushDetail(dataList, ecportOrClick, dateType, finalI);
                    scrollToShow(finalI);
                }
            });
        }
        setPushDetail(dataList, ecportOrClick, dateType, 0);
        sv_expose.scrollBy(sv_expose.getWidth(), 0);
    }

    private void scrollToShow(int position) {
        recyclerView.scrollToPosition(position);
    }

    /**
     * 图形画完，开始列表
     */
    public void setPushDetail(List<GetPushDataRes.DataBean> dataList, int ecportOrClick, int dateType, int finalI) {
        if (adapter == null) {
            adapter = new PushStatisticDataFragmentAdapter(myActivity, dataList, ecportOrClick, dateType, finalI);
            recyclerView.setAdapter(adapter);
        } else {
            adapter.setDate(dataList, ecportOrClick, dateType, finalI);
        }
    }
}
